#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<stack>
using namespace std;
class Solution {
public:
    string decodeString(string s) {
        stack<string> st;
        for (int i = 0; i < s.length(); i++) {
            if (s[i] != ']') {
                string tmp;
                tmp = tmp + s[i];
                st.push(tmp);
            }
            else {
                string tmp;
                while (st.top() != "[") {
                    tmp = st.top() + tmp;
                    st.pop();
                }
                st.pop();
                string num;
                while (!st.empty() && isdigit((st.top())[0])) {
                    num = st.top() + num;
                    st.pop();
                }
                string str;
                int count = stoi(num);
                while (count) {
                    str += tmp;
                    --count;
                }
                st.push(str);
            }
        }
        string res;
        while (!st.empty()) {
            res = st.top() + res;
            st.pop();
        }
        return res;
    }
};
